﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using BLL;
using ET;

namespace Presentation.Page
{
    public partial class liquidation_asset : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtLiquidationAsset;
        private DataTable dtLiquidationAsset_Detail_Edit;
        private DataTable dtLiquidationAssetExport;
        private DataTable dtSearch;
        private DataTable dtAutoComplete;
        protected string SuggestionList;
        protected string StrAlert = "";
        private BLL_LiquidationAsset clsLiquidationAsset = new BLL_LiquidationAsset();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            Autocomplate();
            DisableTexBox();
            imgSave.Visible = false;
            if (!IsPostBack)
            {
                BindGridLiquidationAsset();
                BindGridLiquidationAssetExport();
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            BindGridLiquidationAsset();
            BindGridLiquidationAssetExport();
        }
        protected void cmdDuyet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        imgSave.Enabled = false;
                        BindGridLiquidationAsset_Detail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#liquidationasset");
                        upNewUpdatePanel.Update();
                    }
                }
            }
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {

        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, EventArgs e)
        {

        }
        protected void imgSave_Click(object sender, EventArgs e)
        {

        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {

        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Accept" || e.CommandName == "Accept_A" || e.CommandName == "Accept_B" ||
                e.CommandName == "Accept_C" || e.CommandName == "Accept_D" || e.CommandName == "Accept_E")
            {
                string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                BindGridLiquidationAsset_Detail_Edit(int.Parse(SenderId));
                OpenDialog("#liquidationasset");
                upNewUpdatePanel.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                #region[Double-Click]
                Label lbNote = (Label)e.Item.FindControl("Label1");
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                LinkButton linkListName_A = (LinkButton)e.Item.FindControl("linkListName_A");
                LinkButton linkListName_B = (LinkButton)e.Item.FindControl("linkListName_B");
                LinkButton linkListName_C = (LinkButton)e.Item.FindControl("linkListName_C");
                LinkButton linkListName_D = (LinkButton)e.Item.FindControl("linkListName_D");
                LinkButton linkListName_E = (LinkButton)e.Item.FindControl("linkListName_E");

                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");
                string _jsDouble2 = ClientScript.GetPostBackClientHyperlink(linkListName_A, "");
                string _jsDouble3 = ClientScript.GetPostBackClientHyperlink(linkListName_B, "");
                string _jsDouble4 = ClientScript.GetPostBackClientHyperlink(linkListName_C, "");
                string _jsDouble5 = ClientScript.GetPostBackClientHyperlink(linkListName_D, "");
                string _jsDouble6 = ClientScript.GetPostBackClientHyperlink(linkListName_E, "");

                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";

                linkListName_A.Attributes["ondblclick"] = _jsDouble2;
                linkListName_A.Attributes["onclick"] = "return false;";

                linkListName_B.Attributes["ondblclick"] = _jsDouble3;
                linkListName_B.Attributes["onclick"] = "return false;";

                linkListName_C.Attributes["ondblclick"] = _jsDouble4;
                linkListName_C.Attributes["onclick"] = "return false;";

                linkListName_D.Attributes["ondblclick"] = _jsDouble5;
                linkListName_D.Attributes["onclick"] = "return false;";

                linkListName_E.Attributes["ondblclick"] = _jsDouble6;
                linkListName_E.Attributes["onclick"] = "return false;";
                #endregion

                #region[Char]
                

                if (linkListName_C.Text.Length > 14)
                {
                    linkListName_C.Text = linkListName_C.Text.Substring(0, 14).ToString() + "...";
                }
                if (linkListName_C.Text.Length > 14)
                {
                    linkListName_C.Text = linkListName_C.Text;
                }

                if (lbNote.Text.Length > 14)
                {
                    lbNote.Text = lbNote.Text.Substring(0, 14).ToString() + "...";
                }
                if (lbNote.Text.Length >= 14)
                {
                    lbNote.Text = lbNote.Text;
                }
                #endregion
            }
        }
        protected void ListViewExport_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
        //-----------pagger-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            BindGridLiquidationAsset();
        }
        #endregion

        #region[Function]
        protected void BindGridLiquidationAsset()
        {
            dtSearch = clsLiquidationAsset.Search(txtSearch.Text);
            if (txtSearch.Text == "")
            {
                dtLiquidationAsset = clsLiquidationAsset.BindGridLiquidationAsset();
                if (dtLiquidationAsset != null && dtLiquidationAsset.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtLiquidationAsset;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
                else
                {
                    ListViewAll.Visible = false;
                    DataPagerListAll.Visible = false;
                }
            }
            else
            {
                if (dtSearch != null && dtSearch.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtSearch;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
            }
        }
        protected void BindGridLiquidationAssetExport()
        {
            dtSearch = clsLiquidationAsset.Search(txtSearch.Text);
            if (txtSearch.Text == "")
            {
                dtLiquidationAssetExport = clsLiquidationAsset.BindGridLiquidationAsset();
                if (dtLiquidationAssetExport != null && dtLiquidationAssetExport.Rows.Count > 0)
                {
                    ListViewExport.DataSource = dtLiquidationAssetExport;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
                else
                {
                    ListViewExport.Visible = false;
                }
            }
            else
            {
                if (dtSearch != null && dtSearch.Rows.Count > 0)
                {
                    ListViewExport.DataSource = dtSearch;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
            }
        }
        protected void BindGridLiquidationAsset_Detail_Edit(int id)
        {
            dtLiquidationAsset_Detail_Edit = clsLiquidationAsset.BindGridLiquidationAsset_Detail_Edit(id);
            if (dtLiquidationAsset_Detail_Edit != null && dtLiquidationAsset_Detail_Edit.Rows.Count > 0)
            {
                lbID.Text = dtLiquidationAsset_Detail_Edit.Rows[0]["STT"].ToString();
                lbIDXoa.Text = dtLiquidationAsset_Detail_Edit.Rows[0]["STT"].ToString();
                txtTenTaiSan.Text = dtLiquidationAsset_Detail_Edit.Rows[0]["Tên tài sản"].ToString();
                txtTongNguon.Text = String.Format("{0:0,00}", dtLiquidationAsset_Detail_Edit.Rows[0]["Tổng nguồn"]).Replace(".", ",");
                txtGiaTriKhauHao.Text = String.Format("{0:0,00}",dtLiquidationAsset_Detail_Edit.Rows[0]["Giá trị khấu hao"]).Replace(".", ",");
                txtGiaTriConLai.Text = String.Format("{0:0,00}",dtLiquidationAsset_Detail_Edit.Rows[0]["Giá trị còn lại"]).Replace(".", ",");
                txtGiaTriThanhLy.Text = String.Format("{0:0,00}", dtLiquidationAsset_Detail_Edit.Rows[0]["Giá trị thanh lý"]).Replace(".", ",");
                txtTinhTrang.Text = String.Format("{0:0,00}", dtLiquidationAsset_Detail_Edit.Rows[0]["Tình trạng"]).Replace(".", ",");
                txtNguyenNhan.Text = dtLiquidationAsset_Detail_Edit.Rows[0]["Nguyên nhân"].ToString();
            }
        }
        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsLiquidationAsset.BindGridLiquidationAsset();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["Tên tài sản"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["Tên tài sản"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsLiquidationAsset.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();

                ListViewExport.DataSource = dtSearch;
                ListViewExport.DataBind();
                ListViewExport.Visible = true;

                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;

                ListViewExport.Visible = false;

                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        #endregion

        #region[Texbox]
        protected void DisableTexBox()
        {
            txtTenTaiSan.ReadOnly = true;
            txtTongNguon.ReadOnly = true;
            txtGiaTriKhauHao.ReadOnly = true;
            txtGiaTriThanhLy.ReadOnly = true;
            txtGiaTriConLai.ReadOnly = true;
            txtTinhTrang.ReadOnly = true;
            txtNguyenNhan.ReadOnly = true;
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion
    }
}